package org.winFame;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import org.App;
import org.winFame.dispy.ThreadGetData;

import java.awt.*;
import java.io.File;
import java.net.URI;

public class HboxTop {
    public static HBox hBox = new HBox ();
    public static TextField textField = new TextField();
    public static boolean isAutoDown = false;
    public static boolean isAutoAllDown = false;
    public static boolean offOn = true;
    public static Button autoDown = new Button();
    public static Button autoAllDown = new Button();
    public static String toKeyWord = "";
    static {
        init();
    }
    public static void init() {
        hBox.setStyle("-fx-background-color: #4d504d;");
        hBox.setPrefHeight(50);
        hBox.setPrefWidth(App.stage.getWidth());
        hBox.setAlignment(Pos.CENTER_LEFT);
        hBox.setPadding(new Insets(5));

//        textField.setDisable(offOn);
        textField.setPromptText("输入搜索内容");
        textField.setPrefWidth(300);
        textField.setPrefHeight(50);
        textField.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");

        Button search = new Button();
        search.setPrefWidth(200);
        search.setPrefHeight(50);
        search.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        search.setText("GO");
//        search.setDisable(offOn);

        Button up = new Button();
        up.setPrefWidth(200);
        up.setPrefHeight(50);
        up.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        up.setText("-");

        Button down = new Button();
        down.setPrefWidth(200);
        down.setPrefHeight(50);
        down.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        down.setText("+");

        Button home = new Button();
        home.setPrefWidth(200);
        home.setPrefHeight(50);
        home.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        home.setText("Home");

        autoDown.setPrefWidth(250);
        autoDown.setPrefHeight(50);
        autoDown.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        autoDown.setText("自动下载当前页：关");
        autoDown.setDisable(offOn);

        autoAllDown.setPrefWidth(250);
        autoAllDown.setPrefHeight(50);
        autoAllDown.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        autoAllDown.setText("自动下载所有页数：关");
        autoAllDown.setDisable(offOn);

        Button setTing = new Button();
        setTing.setPrefWidth(250);
        setTing.setPrefHeight(50);
        setTing.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        setTing.setText("Settings");

        Button openURL = new Button();
        openURL.setPrefWidth(200);
        openURL.setPrefHeight(50);
        openURL.setStyle("-fx-border-color: #4d504d;-fx-font-size: 14px");
        openURL.setText("Me");


        hBox.getChildren().addAll(
                textField,
                search,
                home,
                up,
                down,
                autoDown,
                autoAllDown,
                setTing,
                openURL

        );
        openURL.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Desktop.getDesktop().browse(new URI("https://www.snailcatmall.com/"));
                            Desktop.getDesktop().browse(new URI("https://gitee.com/dashboard/projects"));
                        } catch (Exception e) {
                        }
                    }
                    }).start();
            }
        });
        setTing.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                DirectoryChooser directoryChooser = new DirectoryChooser();
                File selectedDirectory = directoryChooser.showDialog(App.stage);
                if(selectedDirectory != null){
                    System.out.println("配置下载文件存放地址： " + selectedDirectory.getAbsolutePath());
                    App.downLoadPath = selectedDirectory.getAbsolutePath()+"\\";
                    HboxTop.offOn = false;
//                    textField.setDisable(offOn);
//                    search.setDisable(offOn);
                    autoDown.setDisable(offOn);
//                    autoAllDown.setDisable(offOn);
                    App.keyWod = false;
                    Thread t = new Thread(new ThreadGetData());
                    t.start();
                }
            }
        });
        autoDown.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                HboxTop.isAutoAllDown = false;
                if(!isAutoDown){
                    HboxTop.isAutoDown = true;
                    autoDown.setText("自动下载当前页：开");
                }else {
                    HboxTop.isAutoDown = false;
                    autoDown.setText("自动下载当前页：关");
                }
                Thread t = new Thread(new ThreadGetData());
                t.start();
            }
        });
        autoAllDown.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                HboxTop.isAutoDown = false;
                if(!isAutoAllDown){
                    HboxTop.isAutoAllDown = true;
                    autoAllDown.setText("自动下载所有页数：开");
                }else {
                    HboxTop.isAutoAllDown = false;
                    autoAllDown.setText("自动下载所有页数：关");
                }
                Thread t = new Thread(new ThreadGetData());
                t.start();
            }
        });

        up.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                ThreadGetData.pageIndex--;
                Thread t = new Thread(new ThreadGetData());
                t.start();
            }
        });
        down.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                ThreadGetData.pageIndex++;
                Thread t = new Thread(new ThreadGetData());
                t.start();
            }
        });

        home.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                ThreadGetData.pageIndex = 1;
                App.keyWod = false;
                Thread t = new Thread(new ThreadGetData());
                t.start();
            }
        });
        search.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(textField.getText().equals("")){
                    App.keyWod = false;
                    App.stage.setTitle("www.snailcatmall.com - 搜索内容不能为空"+" 下载队列： "+App.downLen);
                }else{
                    HboxTop.toKeyWord = textField.getText();
                    textField.setText("");
                    ThreadGetData.pageIndex = 1;
                    App.keyWod = true;
                    Thread t = new Thread(new ThreadGetData());
                    t.start();
                }
            }
        });


        App.stage.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                hBox.setPrefWidth((Double) newValue);
            }
        });
    }
}
